from typing import List

from sqlalchemy.orm import Session

from core.entity.do.models import CmsGaodeFenceTerminal
from core.entity.vo.view_models import CmsGaodeFenceTerminalVO
from utils.uuid import UUID


class CmsGaodeFenceTerminalDao:
    """
    高德终端dao
    """

    @classmethod
    def get_cms_gaode_fence_terminal_by_id(cls, db: Session, id: int):
        """
        根据id查询高德终端信息
        :param db: orm对象
        :param id: 主键ID
        :return: 高德终端信息对象
        """
        cms_gaode_fence_terminal = db.query(CmsGaodeFenceTerminal) \
            .filter(CmsGaodeFenceTerminal.id == id) \
            .first()

        return cms_gaode_fence_terminal

    @classmethod
    def get_cms_gaode_fence_terminal_by_fence_id(cls, db: Session, fence_id: str):
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param fence_id: 围栏id
        :return: 高德终端信息对象
        """
        cms_gaode_fence_terminal_list = db.query(CmsGaodeFenceTerminal) \
            .filter(CmsGaodeFenceTerminal.fence_id == fence_id) \
            .all()
        return cms_gaode_fence_terminal_list

    @classmethod
    def get_cms_gaode_fence_terminal_by_fence_id_and_ter(cls, db: Session, fence_id: str, terminal_id: str):
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param fence_id: 围栏id
        :return: 高德终端信息对象
        """
        cms_gaode_fence_terminal = db.query(CmsGaodeFenceTerminal) \
            .filter(CmsGaodeFenceTerminal.fence_id == fence_id) \
            .filter(CmsGaodeFenceTerminal.terminal_id == terminal_id) \
            .first()
        return cms_gaode_fence_terminal

    @classmethod
    def get_cms_gaode_fence_terminal_by_terminal_id(cls, db: Session, terminal_id: str):
        """
        根据终端id查询高德终端信息
        :param db: orm对象
        :param terminal_id: 终端id
        :return: 高德终端信息对象
        """
        cms_gaode_fence_terminal_list = db.query(CmsGaodeFenceTerminal) \
            .filter(CmsGaodeFenceTerminal.terminal_id == terminal_id) \
            .all()
        return cms_gaode_fence_terminal_list

    @classmethod
    def add_cms_gaode_fence_terminal_dao(cls, db: Session, cms_gaode_fence_terminal: CmsGaodeFenceTerminalVO):
        """
        新增高德终端
        :param db: orm对象
        :param cms_gaode_fence_terminal: 高德终端对象
        :return:
        """
        model_dict = CmsGaodeFenceTerminal(**cms_gaode_fence_terminal.dict())
        db.add(model_dict)
        # db.flush()

        return model_dict

    @classmethod
    def add_cms_gaode_fence_terminal(cls, db: Session, cms_gaode_fence_terminal: CmsGaodeFenceTerminal):
        """
        新增高德终端
        :param db: orm对象
        :param cms_gaode_fence_terminal: 高德终端对象
        :return:
        """
        db.add(cms_gaode_fence_terminal)

    @classmethod
    def add_cms_gaode_fence_terminal_dao_list(cls, db,
                                              cms_gaode_fence_terminal_list: List[CmsGaodeFenceTerminalVO]):
        uuid = UUID()
        for cms_gaode_fence_terminal in cms_gaode_fence_terminal_list:
            cms_gaode_fence_terminal.id = uuid.uuid()
            model_dict = CmsGaodeFenceTerminal(**cms_gaode_fence_terminal.dict())
            db.add(model_dict)
        db.commit()

    @classmethod
    def edit_cms_gaode_fence_terminal_dao(cls, db: Session, cms_gaode_fence_terminal: dict):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param cms_gaode_fence_terminal: 需要更新的参数配置字典
        :return:
        """
        db.query(CmsGaodeFenceTerminal) \
            .filter(CmsGaodeFenceTerminal.id == cms_gaode_fence_terminal.get('id')) \
            .update(cms_gaode_fence_terminal)

    @classmethod
    def delete_cms_gaode_fence_terminal_dao(cls, db: Session, service_id: str, fence_id: str, terminal_id: str):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param service_id: 服务编码
        :param fence_id: 围栏编码
        :param terminal_id: 终端编码
        :return:
        """
        db.query(CmsGaodeFenceTerminal) \
            .filter(CmsGaodeFenceTerminal.service_id == service_id and CmsGaodeFenceTerminal.fence_id == fence_id
                    and CmsGaodeFenceTerminal.terminal_id == terminal_id) \
            .delete()

    @classmethod
    def delete_cms_gaode_fence_terminal_by_fence_id(cls, db: Session, fence_id: str):
        """
        编辑参数配置数据库操作
        :param db: orm对象
        :param service_id: 服务编码
        :param fence_id: 围栏编码
        :param terminal_id: 终端编码
        :return:
        """
        db.query(CmsGaodeFenceTerminal) \
            .filter(CmsGaodeFenceTerminal.fence_id == fence_id) \
            .delete()
